<?php
/**
 * Created by PhpStorm.
 * User: 76683
 * Date: 2018/7/16
 * Time: 22:59
 */

namespace app\api\controller;

use app\api\model\WeAds;
use app\api\model\WeDayTopic;
use app\api\model\WeReport;
use think\Db;
use think\Exception;
use think\Request;
use app\api\model\WeTopic as WeTopicModel;
use app\api\until\Token as CopyToken;
use app\api\model\BadWords;
use app\api\model\WeCategory;
use app\api\model\WeTopicDetail;

class WeTopic extends Base
{
    protected $weDayTopic = null;

    protected $weTopic = null;

    protected $weAds = null;

    protected $weReport = null;

    protected $weTopicDetail = null;

    function __construct(Request $request = null)
    {
        $this->weDayTopic = new WeDayTopic();
        $this->weTopic = new WeTopicModel();
        $this->weAds = new WeAds();
        $this->weTopicDetail = new WeTopicDetail();
        parent::__construct($request);
    }

    /**
     *      * 树洞首页接口
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $data = $this->request->post();
        if (isset($data['token']) && $data['token']) {
            $tokenModel = new CopyToken();
            $res = $tokenModel->decodeToken($data['token']);
            if ($res === false) {
                list($this->code, $this->msg, $this->data) = [10000, '请登录', []];
                $this->__destruct();
            } else {
                $this->uid = $res;
            }
        }
        $todayTopic = $this->weDayTopic->todayTopic($this->uid);
        $hotTopic = $this->weTopic->hotTopic($this->uid);
        $data['category_id'] = \app\api\model\WeCategory::pubCate();
        $topicList = $this->weTopic->topicList($data, $this->uid);
        $ads = $this->weAds->dayAds();
        list($this->code, $this->msg, $this->data) = [1, '列表获取成功', ['today' => $todayTopic, 'hot' => $hotTopic, 'ads' => $ads, 'topic_list' => $topicList]];
    }

    /**
     * 我的话题接口
     * @throws \think\exception\DbException
     */
    public function userTopic()
    {
        $uid = $this->uid;
        $data = $this->request->post();
        if (!$uid) {
            list($this->code, $this->msg) = [10000, '请登录'];
            return;
        }
        $hot = isset($data['hot']) ? $data['hot'] : false;
        $user_topic_list = $this->weTopic->userTopicList($uid, $data, $hot);
        list($this->code, $this->msg, $this->data) = [1, '用户话题请求成功', $user_topic_list];
        return;
    }

    /**
     * 最热门
     * @throws \think\exception\DbException
     */
    public function theHot()
    {
        $uid = $this->uid;
        $query = $this->request->post();
        $category_id = isset($query['category_id']) ? $query['category_id'] : WeCategory::pubCate();
        $hotTopic = $this->weTopic->theHot($uid, $category_id);
        list($this->code, $this->msg, $this->data) = [1, '最热门话题请求成功', $hotTopic];
        return;
    }

    public function addTopic()
    {
        $data = $this->request->post();
        $request_time = $this->request->time();
        $res = $this->validate($data, 'WeTopic.add');
        if (true !== $res) {
            list($this->code, $this->msg) = [0, $res];
            return;
        }
        $data['user_id'] = $this->uid;
        //间隔时间
        $max_time = WeTopicModel::where(['user_id' => $this->uid])->max('create_time');

        $time = $request_time - $max_time;
        if ($time < 60 * 10) {
            list($this->code, $this->msg) = [0, '您还有' . intval((60 * 10 - $time) / 60) . '分' . (60 * 10 - $time) % 60 . '秒可以发布新的话题'];
            return;
        }

        //违禁词过滤
        $data['content'] = BadWords::badWordsFilter($data['content']);
        $result = WeTopicModel::create($data);
        if ($result->id) {
            list($this->code, $this->msg) = [1, '话题发布成功'];
            return;
        } else {
            list($this->code, $this->msg) = [0, '话题发布失败'];
            return;
        }
    }

    public function deleteTopic()
    {
        $data = $this->request->post();
        $uid = $this->uid;
        if (!$uid) {
            list($this->code, $this->msg) = [10000, '请登录'];
            return;
        }
        $res = $this->validate($data, ['id' => 'require|integer'], ['id.require' => '请选择正确话题', 'id.integer' => '请选择正确话题']);
        if (true !== $res) {
            list($this->code, $this->msg) = [0, $res];
        }
        $result = $this->weTopic->deleteTopic($uid, $data['id']);
        if ($result) {
            list($this->code, $this->msg) = [1, '删除成功'];
            return;
        } else {
            list($this->code, $this->msg) = [0, '删除失败'];
            return;
        }
    }


    /**
     * 举报话题
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function report()
    {
        $data = $this->request->post();
        $uid = $this->uid;
        if (!$uid) {
            list($this->code, $this->msg) = [0, '请登录'];
        }
        $res = $this->validate($data, ['value' => 'require|integer'], ['value.require' => '请选择正确话题', 'value.integer' => '请选择正确话题']);
        if (true !== $res) {
            list($this->code, $this->msg) = [0, $res];
            return;
        }
        //是否存在话题
        $topic_info = $this->weTopic->getTopicInfo($data['value']);
        if (empty($topic_info)) {
            list($this->code, $this->msg) = [0, '错误话题ID'];
            return;
        }
        //是否已经举报过
        $report = new WeReport();
        $data['user_id'] = $uid;
        $data['type'] = 1;
        $exists = $report->alreadyReport($data);
        if ($exists) {
            list($this->code, $this->msg) = [0, '您已经举报过此话题'];
            return;
        }
        try {
            Db::startTrans();
            //更新话题字段
//            $update = $this->weTopic->reportTopic($data['value']);
//            if (!$update) {
//                throw new Exception('举报失败');
//            }
            //写入举报信息表
            $result = $report->reportTopic($data);
            if (!$result) {
                throw new Exception('举报失败');
            }
            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            list($this->code, $this->msg) = [0, $e->getMessage()];
            return;
        }
        list($this->code, $this->msg) = [1, '话题举报成功'];
        return;
    }

    public function theReply(){
        //获取最新回复列表 时间倒序
        $data = $this->weTopicDetail->replyDesc();
            list($this->code,$this->msg,$this->data) = [1,'列表获取成功',$data];
        return;
    }


    public function hotList(){
        $data_list = $this->weTopicDetail->hotList();
        list($this->code,$this->msg,$this->data) = [1,'列表获取成功',$data_list];
        return;
    }
}